<?php
require_once("Exercises/Model/Exercise.php");
require_once("login/src/Model/LoginValidation.php");
require_once("Database/Model/DatabaseConnection.php");

class ExerciseDAL
{
    /**
    *@var LoginValidation
    **/
    private $loginValidation;
     /**
    *@var DatabaseConnection
    **/
	private $databaseConnection;

	public function __construct()
	{
        $this->loginValidation = new LoginValidation();
		$this->databaseConnection = new DatabaseConnection();
	}

    /**
    *@param int
    *@return array exercises
    **/
	public function getExerciseList($exerciseID = null)
	{
        $userID =  $this->loginValidation->getLoggedInUserID();
        if($exerciseID  != null)
        {
            $sql = "SELECT `ExerciseID`, MuscleGroupID, Name, Description FROM exercise WHERE UserInfoID = $userID AND ExerciseID = $exerciseID";

            $exercise = $this->ExerciseList($sql);      
            if(isset($exercise[0]))
            {
                return $exercise[0];
            }
            else
            {
            
                return;
            }
        }
        else
        {
		    $sql = "SELECT `ExerciseID`, MuscleGroupID, Name, Description FROM exercise WHERE UserInfoID = $userID ORDER BY MuscleGroupID";
            return $this->ExerciseList($sql);
        }
		
	}

    /**
    *@param string sql
    *@return array exercises
    **/
	public function ExerciseList($sql)
	{
		$mysqli = new mysqli("workoutdiary-185687.mysql.binero.se", "185687_ac16366", "password", "185687-workoutdiary", 3306);


		$stmt = $mysqli->prepare($sql);
            if ($stmt == FALSE) 
            {
              //  throw new \Exception($sql);
            }

            
            $result = $stmt->execute();
            if ($result == FALSE) 
            {
              //  throw new \Exception("execute of [$sql] failed " . $stmt->error);
            }

            $result = $stmt->bind_result($exerciseID, $muscleGroupID, $name, $description);
            if ($result == FALSE)
            {
              //   throw new \Exception("execute of [$sql] failed " . $stmt->error);
            }

            $exerciseList = array();

            while ($stmt->fetch()) 
            {
                $musclegroupName = $this->getMuscleGroupNameOnID($muscleGroupID);
                
                $exerciseList[] = Exercise::createNewExerciseFromDatabase($musclegroupName,
                                                                          $muscleGroupID,
                													      $name, 
                													      $description, 
                                                                          $exerciseID);
            }
            $mysqli->close();
            return $exerciseList;
	}


    /**
    *@param int
    *@return string
    **/
    public function getMuscleGroupNameOnID($muscleGroupID)
    {
        $mysqli = new mysqli("workoutdiary-185687.mysql.binero.se", "185687_ac16366", "password", "185687-workoutdiary", 3306);
        $sql = "SELECT  `Name` FROM mousclegroups WHERE `MuscleGroupID` = ?";
        
        $stmt = $mysqli->prepare($sql);
        if ($stmt == FALSE) 
        {
            throw new Exception("prepare of [$sql] failed " . $this->mysqli->error);
        }

        $result = $stmt->bind_param("i", $muscleGroupID);
        if ($result == FALSE)
        {
                throw new Exception("execute of [$sql] failed " . $stmt->error);
        }
        
        $result = $stmt->execute();
        if ($result == FALSE) 
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $result = $stmt->bind_result($name);
        if ($result == FALSE) 
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }
        $theName = "";
        while ($stmt->fetch()) 
        {
            $theName = $name;
        }   

        $mysqli->close();
        return $name;
    }

    /**
    *@param Exercise
    *@param int
    **/
	public function saveNewExercise(Exercise $exercises, $exerciseID = null)
	{
		$mysqli = new mysqli("workoutdiary-185687.mysql.binero.se", "185687_ac16366", "password", "185687-workoutdiary", 3306);

        $mgID = $exercises->getMusclegroupID();
        $name = $exercises->getExerciseName();
        $description = $exercises->getExerciseDescription();

        if($exerciseID != null)
        {
            $sql = "UPDATE exercise
                    SET MuscleGroupID = ?, 
                        Name = ?, 
                        Description = ?
                    WHERE ExerciseID = ?";

                $statement = $mysqli->prepare($sql);
                if ($statement === FALSE) 
                {
                    throw new Exception("prepare of $sql failed " . $this->mysqli->error);
                }

                if ($statement->bind_param("issi",  $mgID,
                                                    $name,
                                                    $description,
                                                    $exerciseID) === FALSE) 
                {
                    throw new Exception("bind_param of $sql failed " . $statement->error);
                }

                if ($statement->execute() === FALSE) 
                {
                    throw new Exception("execute of $sql failed " . $statement->error);
                }
        }
        else
        { 
    	    $sql = "INSERT INTO exercise 
                                (
                                    UserInfoID, 
                                	MuscleGroupID, 
                                	Name, 
                                	Description
                                ) 
                                VALUES(?, ?, ?, ?)";

                $statement = $mysqli->prepare($sql);
                if ($statement === FALSE) 
                {
                    throw new Exception("prepare of $sql failed " . $this->mysqli->error); 
                }


                $exerciseID = $exercises->getExerciseID();
                $userID = $this->loginValidation->getLoggedInUserID();
                $muscleGroupID = $exercises->getMusclegroupID();
                $exerciseName = $exercises->getExerciseName(); 
                $exerciseDescription = $exercises->getExerciseDescription();
     
                //http://www.php.net/manual/en/mysqli-stmt.bind-param.php
                if ($statement->bind_param("iiss",  $userID,
                                                    $muscleGroupID, 
                                                    $exerciseName, 
                                                    $exerciseDescription) === FALSE) 
                {
                    throw new Exception("bind_param of $sql failed " . $statement->error);
                }

                if ($statement->execute() === FALSE) 
                {
                    throw new Exception("execute of $sql failed " . $statement->error);
                }
        }
		$mysqli->close();
	}


    /**
    *@param int
    **/
    public function DeleteExercise($exerciseID)
    {
        $sql = "DELETE FROM exercise WHERE `ExerciseID` = $exerciseID"; 

        $mysqli = new mysqli("workoutdiary-185687.mysql.binero.se", "185687_ac16366", "password", "185687-workoutdiary", 3306);

        $stmt = $mysqli->prepare($sql);
        if ($stmt == FALSE) 
        {
            throw new Exception("prepare of [$sql] failed " . $this->mysqli->error);
        }

        $result = $stmt->execute();
        if ($result == FALSE) 
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $mysqli->close();

    }
}